Skip to content

prometheus: start new counter and histogram series at zero - #237

Closed
sathvik09 wants to merge 2 commits into
mainfrom
fix/prometheus-zero-new-series
Closed

sathvik09 wants to merge 2 commits into
mainfrom
fix/prometheus-zero-new-series

Conversation

@sathvik09

Copy link
Copy Markdown
Contributor

What

increase() and rate() count the change between scraped samples, never a series' first sample. The Prometheus handler exposed a new counter at its first value and expired idle series after MetricTimeout. So a counter first scraped at 3 lost those 3, and a series expired and re-created at 1 read as no change at all.

                 scrapes            increase()   actual
before (sparse)  1 1 1 1 · · 1 1 1      0           2
after            0 0 1 1 · · 0 0 1      2           2

New and re-created counter and histogram series are now published as 0 for the new Handler.ZeroPeriod before their real value:

  • Time-based, not per scrape. Every scraper that reads during the window sees 0. Showing 0 for one read only would give the zero to whichever scraper came first, such as one of two HA replicas or a curl /metrics.
  • Defaults to 2 minutes, twice the common 60s scrape interval (Prometheus's own default). A late scrape, or a target handed to another collector, can skip a window only one interval long.
  • Delayed, not lost. Updates still accumulate during the window. A series isn't expired during its zero period, and its value is published for a full MetricTimeout afterwards.
  • Gauges are unchanged. A negative ZeroPeriod turns it off.

Why

Busy series hit this once per process. Sparse, high-cardinality series hit it every time they come back. A good example is a response counter labelled by a per-customer ID, where each series sees a few requests, goes idle, expires and returns. Such a counter can read several percent low against the same counter sent to a statsd backend, while a request counter on the same process, labelled only by endpoint, matches. Raising MetricTimeout reduces how often series come back, but every new series still loses its first value.

Tests

prometheus/zero_test.go:

  • TestZeroPeriodCountsEveryIncrement simulates 30 minutes of traffic scraped every 15s: one busy series, plus three per-customer series that are expired between bursts. It sums increase() per series the way Prometheus does. With the zero period off, it counts 1907 of 1914 increments. With the default, it counts all 1914.
  • TestZeroPeriodIsTimeBased: repeated scrapes inside the window all see 0 for the counter, buckets, _sum and _count, and the gauge is unaffected. The real values appear once the window ends.
  • TestZeroPeriodOutlivesExpiry: a series is kept through its zero period and for a full MetricTimeout after it.

The handler's wall clock now goes through a package-level now so these tests can step time.

Ten existing tests read values right after the first update, to check names and buckets. They now set ZeroPeriod: -1.

go test ./..., go test -race ./prometheus/, go vet and gofmt pass. golangci-lint was not run locally.

HISTORY.md has a v5.12.0 entry, and README.md documents ZeroPeriod.

🤖 Generated with Claude Code

sathvik09 and others added 2 commits October 28, 2026 02:23
increase() and rate() count the change between samples, never a
series' first sample. The handler exposed a new counter at its first
value and expired idle series after MetricTimeout, so a counter first
scraped at 3 lost those 3, and a series expired and re-created at 1
read as no change. Busy series lose this once per process. Sparse,
high-cardinality series, such as a counter labelled by a per-customer
ID that sees a few requests and goes idle, lose it every time they
come back, and read several percent low against the same counter sent
to a statsd backend.

New and re-created counter and histogram series are now published as
0 for Handler.ZeroPeriod before their real value. The period is
measured on the wall clock, not per scrape, so every scraper sees the
zero. It defaults to 2 minutes, twice the common 60s scrape interval,
so a late scrape or a target moving between collectors still lands in
it. A series is not expired during its zero period, and its value is
published for a full MetricTimeout afterwards, so counts are delayed
rather than lost. Gauges are unchanged; a negative ZeroPeriod turns
this off.

Tests that read values right after the first update now set
ZeroPeriod: -1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant